﻿//
// MeasuredItemRepository.cs
//
// Authors:
//    Lasse Andreas Hauge Petersen <09547@iha.dk>
//    Søren Flohr Nielsen <10638@iha.dk>
//    Claus Jørgensen <10229@iha.dk>
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Data.Objects;
using System.Diagnostics.Contracts;

namespace RSD.Data
{
    /// <summary>
    /// Represents a MeasuredItem Repository.
    /// </summary>
    [ContractClass(typeof(Contracts.IMeasuredItemRepositoryContract))]
    public interface IMeasuredItemRepository : IRepository<MeasuredItem>
    {
        /// <summary>
        /// Searches for measured items over a timespan, and a <see cref="T:ItemClassification"/>.
        /// </summary>
        /// <param name="startDate">The start date.</param>
        /// <param name="endDate">The end date.</param>
        /// <param name="itemClassification">The item classification.</param>
        /// <returns>The matched items.</returns>
        IEnumerable<MeasuredItem> Search(DateTime startDate, DateTime endDate, ItemClassification itemClassification);
    }

    /// <summary>
    /// Represents a MeasuredItem Repository.
    /// </summary>
    public sealed class MeasuredItemRepository : RepositoryBase<MeasuredItem>, IMeasuredItemRepository
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MeasuredItemRepository"/> class.
        /// </summary>
        /// <param name="context">The context.</param>
        public MeasuredItemRepository(IObjectContext context)
            : base(context)
        {
            Contract.Requires(context != null);
        }

        /// <summary>
        /// Searches for measured items over a timespan, and a <see cref="T:ItemClassification"/>.
        /// </summary>
        /// <param name="startDate">The start date.</param>
        /// <param name="endDate">The end date.</param>
        /// <param name="itemClassification">The item classification.</param>
        /// <returns>The matched items.</returns>
        public IEnumerable<MeasuredItem> Search(DateTime startDate, DateTime endDate, ItemClassification itemClassification)
        {
            return FindBy(m => m.TimeMeasured >= startDate
                            && m.TimeMeasured <= endDate
                            && m.ItemClassificationId == itemClassification.ItemClassificationId);
        }
    }
}
